package com.jiangjiang.eshop.auth.operation;

import com.jiangjiang.eshop.auth.domain.PriorityEntity;
import com.jiangjiang.eshop.auth.dto.PriorityNode;
import com.jiangjiang.eshop.auth.service.PriorityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName QueryAuthorizedPriorityOperation
 * @Description TODO
 * @Author jhw
 * @Date 2021/7/21 10:05
 * @Version 1.0
 **/
@Component
@Scope("prototype")
public class QueryAuthorizedPriorityOperation implements PriorityOperation<Boolean> {

    private Long accountId;

    @Autowired
    private PriorityService priorityService;

    @Override
    public Boolean doExecute(PriorityNode node) throws Exception {
        List<PriorityNode> targetChildren = new ArrayList<>();
        List<PriorityEntity> children = priorityService.listAuthorizedTreeByAccountId(accountId, node.getId());
        for (PriorityEntity child : children) {
            PriorityNode childNode = new PriorityNode();
            BeanUtils.copyProperties(child, childNode);
            childNode.execute(this);
            targetChildren.add(childNode);
        }
        node.setChildren(targetChildren);
        return true;
    }

    public Long getAccountId() {
        return accountId;
    }

    public void setAccountId(Long accountId) {
        this.accountId = accountId;
    }
}
